package main

import (
	"fmt"
	"time"
)

// struct{} zero base address
func worker(done <-chan struct{}) {

	for {
		select {
		case <-done:
			fmt.Println("ready to exit")
			return
		default:
			fmt.Println("working")
			time.Sleep(time.Second)
		}
	}
}

// 基于channel方式退出goroutine
// 理论基础 CSP 模型
func main() {
	done := make(chan struct{})
	go worker(done)
	time.Sleep(time.Second * 5)
	// csp 基于通讯, 发送信号通知退出
	done <- struct{}{}
	fmt.Println("over")
}
